package utils;

import java.io.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;


public class LeerMail {

  public LeerMail(){
  }

  public static void main (String args[])
      throws Exception {
    String host = "170.210.129.141";

    // Get system properties
    Properties props = System.getProperties();
    props.put("imap", host);

    // Get session
    Session session = Session.getDefaultInstance(props, null);

    // Get the store
    Store store = session.getStore("imap");
    store.connect(host, "info", "eF84yo");

    // Get folder
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_ONLY);

    System.out.println("Cantidad total de mails= "+folder.getMessageCount() );
    System.out.println("Cantidad de mails no leidos= "+folder.getUnreadMessageCount() );

    // Close connection
    folder.close(false);
    store.close();
  }

  public int[] getCantidadMailsNoLeidos(String host,String user,String pass)
    throws Exception {

  int[] cantidad=new int[2];

  Properties props = System.getProperties();
  props.put("imap", host);

  Session session = Session.getDefaultInstance(props, null);

  Store store = session.getStore("imap");

  store.connect(host, user, pass);

  // Obtiene la carpeta
  Folder folder = store.getFolder("INBOX");
  folder.open(Folder.READ_ONLY);

  //System.out.println("Cantidad de mails no leidos= "+folder.getMessageCount() );
  //System.out.println("Cantidad de mails no leidos= "+folder.getUnreadMessageCount() );

  cantidad[0]=folder.getNewMessageCount();
  cantidad[1]=folder.getUnreadMessageCount();

  // Cierra la  connexion
  folder.close(false);
  store.close();

  return cantidad;
}


}